/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.meducated.sim.model;

import java.util.HashMap;

/**
 *
 * @author Carina
 */
public class Property<V> {

    /**
     * The full name of this property
     */
    private final String name;
    /**
     * The unique identifier of this property
     */
    private final String id;
    /**
     * The units of this property
     */
    private final String units;
    /**
     * The value of this property at the last update
     */
    private V value;
    /**
     * The time of the last update and the difference in time from the last
     * update and the previous update
     */
    private double t, dt;
    /**
     * The history previous values of this property as a map of <time, value>
     * pairs
     */
    private final HashMap<Double, V> history;
    /**
     * The connector for this property which links it to a model and to other
     * factors which may influence its value
     */
    private final PropertyConnector<V> connector;

    /**
     * 
     * @param id
     * @param name
     * @param value 
     */
    public Property (String id, String name, V value) {
        this(id, name, "", value);
    }
    
    /**
     *
     * @param id
     * @param name
     * @param value
     */
    public Property(String id, String name, String units, V value) {
        this.id = id;
        this.name = name;
        this.value = value;
        this.units = units;
        this.t = 0;
        this.dt = 0;
        this.history = new HashMap<Double, V>();
        this.connector = new PropertyConnector<V>(this);
    }

    /**
     *
     */
    protected final void setValue(double t, V value) {
        if (t > this.t) {
            history.put(t, this.value);
        }
        this.value = value;
    }

    /**
     *
     * @return
     */
    public V getValue() {
        return this.value;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {
        return id + "=" + this.value + this.units;
    }

    /**
     * @return the history
     */
    public HashMap<Double, V> getHistory() {
        return null;
    }

    /**
     * @return the connector
     */
    public PropertyConnector<V> getConnector() {
        return connector;
    }

    /**
     * 
     * @return 
     */
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 71 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 71 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    /**
     * 
     * @param obj
     * @return 
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Property<V> other = (Property<V>) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) {
            return false;
        }
        return true;
    }
    
}
